package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.model.dto.FileInfo;
import com.settlement.system.model.entity.common.FileByte;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.common.util.common.FileByteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author
 */
@Api(tags = "财务系统接口",description = "财务系统接口")
@RestController
@RequestMapping(value = "/file/api")
@Slf4j
public class FileUpLoadApiController {

    /*@Reference(version = "${dubbo.dl.api.version}",check = false)
    private FileUploadApi fileUploadApi;*/


    @ApiOperation("上传文件")
    @PostMapping("upload")
    public ObjectDataResponse<String> upload(MultipartFile file){
        try {
            FileByte fileByte = FileByteUtils.getSingleFileByte(file);
            FileInfo fileInfo = updateFile(fileByte);
            return new ObjectDataResponse(fileInfo.getPath(),ReturnStatusEnum.IMPORT_SUCCESS.getValue(),ReturnStatusEnum.IMPORT_SUCCESS.getText());
        } catch (FileUploadException e) {
            log.error("Api上传文件异常",e);
            return new ObjectDataResponse(ReturnStatusEnum.IMPORT_ERROR.getValue(),ReturnStatusEnum.IMPORT_ERROR.getText());
        } catch (Exception ex){
            log.error("Api上传文件异常",ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(),ReturnStatusEnum.REQUEST_ABNORMAL.getText());
        }
    }

    /**
     * 上传文件
     * @param fileByte 文件信息
     * @return
     * @throws FileUploadException
     */
    private FileInfo updateFile(FileByte fileByte) throws FileUploadException {
        /*return fileUploadApi.upload(fileByte.getContent(),fileByte.getFilePath());*/
        return null;
    }


}
